import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author 何杰
 * @version 1.0
 */
public class BinarySearch {

    public static int BinarySearch(int[] a, int target) {
        int i = 0, j = a.length - 1;
        while(i <= j) {
            int m = (i + j) >>> 1;
            if(target < a[m]) {
                j = m - 1;
            } else if(target > a[m]) {
                i = m + 1;
            } else {
                return m;
            }
        }
        return -1;
    }

    public static int BinarySearchAltertive(int[] a, int target) {
        int i = 0, j = a.length;
        while(i < j) {
            int m = (i + j) >>> 1;
            if(target < a[m]) {
                j = m;
            } else if(target > a[m]) {
                i = m + 1;
            } else {
                return m;
            }
        }
        return -1;
    }

    public static int BinarySearchAlternative2(int[] a, int target) {
        int i = 0, j = a.length;
        while(1 < j - i) {
            int m = (i + j) >>> 1;
            if(a[m] > target) {
                j = m;
            } else {
                i = m;
            }
        }
        if(a[i] == target) {
            return i;
        } else {
            return -1;
        }
    }

    public static int BinarySearchLestmost(int[] a, int target) {
        int i = 0, j = a.length - 1;
        while(i <= j) {
            int m = (i + j) >>> 1;
            if(target <= a[m]) {
                j = m - 1;
            } else {
                i = m + 1;
            }
        }
        return i;
    }

    public static int BinarySearchRightmost(int[] a, int target) {
        int i = 0, j = a.length - 1;
        while(i <= j) {
            int m = (i + j) >>> 1;
            if(target < a[m]) {
                j = m - 1;
            } else {
                i = m + 1;
            }
        }
        return i - 1;
    }

    @Test
    public void test1() {
        int[] a = {3, 4, 9};
        int target = 6;
        int i = Arrays.binarySearch(a, target);
        if(i < 0) {
            int insertIndex = Math.abs(i + 1);
            int[] b = new int[a.length + 1];
            System.arraycopy(a, 0, b, 0, insertIndex);
            b[insertIndex] = target;
            System.arraycopy(a, insertIndex, b, insertIndex + 1, a.length - insertIndex);
            System.out.println(Arrays.toString(b));
        }
    }
    @Test
    @DisplayName("算法成功通过测试")
    public void test() {
        int[] arr = {1, 23, 34, 46, 67, 98, 132, 255};
        System.out.println(BinarySearch(arr, 1));
        System.out.println(BinarySearch(arr, 23));
        System.out.println(BinarySearch(arr, 34));
        System.out.println(BinarySearch(arr, 46));
        System.out.println(BinarySearch(arr, 67));
        System.out.println(BinarySearch(arr, 98));
        System.out.println(BinarySearch(arr, 132));
        System.out.println(BinarySearch(arr, 255));
        System.out.println(BinarySearch(arr, 345));
    }

    @Test
    @DisplayName("算法成功通过测试")
    public void testLeft() {
        int[] arr = {1, 2, 4, 4, 4, 5, 6, 7};
        System.out.println(BinarySearchLestmost(arr, 1));
        System.out.println(BinarySearchLestmost(arr, 2));
        System.out.println(BinarySearchLestmost(arr, 4));
        System.out.println(BinarySearchLestmost(arr, 5));
        System.out.println(BinarySearchLestmost(arr, 6));
        System.out.println(BinarySearchLestmost(arr, 7));
    }

    @Test
    @DisplayName("算法成功通过测试")
    public void testRight() {
        int[] arr = {1, 2, 4, 4, 4, 5, 6, 7};
        System.out.println(BinarySearchRightmost(arr, 1));
        System.out.println(BinarySearchRightmost(arr, 2));
        System.out.println(BinarySearchRightmost(arr, 4));
        System.out.println(BinarySearchRightmost(arr, 5));
        System.out.println(BinarySearchRightmost(arr, 6));
        System.out.println(BinarySearchRightmost(arr, 7));
    }

    @Test
    @DisplayName("算法成功通过测试")
    public void test2() {
        int[] arr = {1, 23, 34, 46, 67, 98, 132, 255};
        System.out.println(BinarySearchAltertive(arr, 1));
        System.out.println(BinarySearchAltertive(arr, 23));
        System.out.println(BinarySearchAltertive(arr, 34));
        System.out.println(BinarySearchAltertive(arr, 46));
        System.out.println(BinarySearchAltertive(arr, 67));
        System.out.println(BinarySearchAltertive(arr, 98));
        System.out.println(BinarySearchAltertive(arr, 132));
        System.out.println(BinarySearchAltertive(arr, 255));
        System.out.println(BinarySearchAltertive(arr, 345));
    }

    public static int longestConsecutive(int[] nums) {
        Arrays.sort(nums);
        for (int num : nums) {
            System.out.print("\nnum=" + num);
        }
        Integer temp = 1, result = 1;
        for(int i = 1; i < nums.length; i++) {
            if(nums[i] - nums[i-1] == 1) {
                temp++;
                result = Math.max(result, temp);
            } else if(nums[i] - nums[i-1] == 0){
                continue;
            } else {
                temp = 1;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {1,0,1,2};
        int count = longestConsecutive(nums);
        System.out.println("\n"+count);
    }
}
